import axios from 'axios'

export type ResType = {
  code: number,
  msg: string,
  data: ResDataType
}
export type ResDataType = {
  [key: string]: any
}


const config = {
  baseURL: 'http://120.78.91.121:4000/v1',
  timeout: 5000,
  validateStatus: function (status: number) {
    return status >= 200 && status < 500; 
  }
}
const service = axios.create(config)


service.defaults.headers.post['Content-Type'] =
  'application/json;charset=UTF-8';

service.interceptors.response.use((res) => {
    
    if (res.status.toString().charAt(0) === '2') {
      return res.data as any
    }
    
})


export function get(url: string, params = {}): Promise<ResDataType> {
  return service<ResDataType>({
      method: 'get',
      url,
      params
  })
}

export function put(url: string, data = {}, params = {}) :Promise<ResDataType>{
  return service<ResDataType>({
      method: 'put',
      url,
      params,
      data,
  })
}

export default service
